Preparations

Load the necessary libraries

library(tidyverse) #for data wrangling
theme_set(theme_classic())
library(grid)
library(patchwork)
library(scales)
library(gridExtra)
load(file='../data/manipulationDatasets.RData')

Graphics infrastructure

  • layers of data driven objects
  • coordinate system
  • scales
  • faceting
  • themes

Motivating data

head(BOD)
summary(BOD)
##       Time           demand     
##  Min.   :1.000   Min.   : 8.30  
##  1st Qu.:2.250   1st Qu.:11.62  
##  Median :3.500   Median :15.80  
##  Mean   :3.667   Mean   :14.83  
##  3rd Qu.:4.750   3rd Qu.:18.25  
##  Max.   :7.000   Max.   :19.80
BOD <- rename(BOD, time = Time)

Layers

  • layers of data driven objects
    • geometric objects to represent data
    • statistical methods to summarize the data
    • mapping of aesthetics
    • position control

geom_point

ggplot(data = BOD, aes(y = demand, x = time)) +
  geom_line(aes(col = time)) +
  geom_point(aes(col = demand, size=demand)) +
  scale_color_viridis_c() +
  guides(color = guide_legend(title = "Time"),
         size = guide_legend(title = "Demand")) +
  labs(x = "Time", y = "Demand")

ggplot(CO2, aes(y = uptake, x = conc)) + 
  geom_point()+
  scale_x_log10(name = "CO2 conc",
    breaks = as.vector(c(1, 2, 5, 10) %o% 10^(-1:2)))

# works well to create nice breaks around 1, 2, 5, and 10 at different log scales!
c(1, 2, 5, 10) %o% 10^(-1:2)
##      [,1] [,2] [,3] [,4]
## [1,]  0.1    1   10  100
## [2,]  0.2    2   20  200
## [3,]  0.5    5   50  500
## [4,]  1.0   10  100 1000

Primary geometric objects

geom_bar

geom_boxplot

geom_line

geom_point

geom_smooth

geom_polygon

geom_tile

geom_raster

Secondary geometric objects

geom_errorbar

geom_pointrange

Coordinate systems

Scales

scale_x_ and scale_y_

Other scales

  • size of points (thickness of lines)
  • shape of points
  • linetype of lines
  • color of lines or points
  • fill of shapes

scale_size

scale_shape

scale_linetype

scale_fill and scale_color

state=data.frame(state.x77, state.region, state.division, state.center) %>%
    select(Illiteracy, state.region, x, y)
head(state)
state %>%
  group_by(state.region) %>%
  summarise(mean_il = mean(Illiteracy),
            se_il = sd(Illiteracy) / sqrt( n() ),
            lwr_il = gmodels::ci(Illiteracy)[2],
            upr_il = gmodels::ci(Illiteracy)[3]) %>%
  ggplot(aes(x = state.region, y = mean_il, fill=state.region)) +
  geom_col() +
  geom_pointrange(aes(ymin = lwr_il, ymax = upr_il)) +
  labs(x = "Region", y = "Mean Illiteracy (%)")
## Warning in ci.numeric(Illiteracy): No class or unkown class. Using default
## calcuation.

## Warning in ci.numeric(Illiteracy): No class or unkown class. Using default
## calcuation.

## Warning in ci.numeric(Illiteracy): No class or unkown class. Using default
## calcuation.

## Warning in ci.numeric(Illiteracy): No class or unkown class. Using default
## calcuation.

## Warning in ci.numeric(Illiteracy): No class or unkown class. Using default
## calcuation.

## Warning in ci.numeric(Illiteracy): No class or unkown class. Using default
## calcuation.

## Warning in ci.numeric(Illiteracy): No class or unkown class. Using default
## calcuation.

## Warning in ci.numeric(Illiteracy): No class or unkown class. Using default
## calcuation.
## `summarise()` ungrouping output (override with `.groups` argument)

Facets

Themes

theme_classic

theme_bw

theme_grey

theme_minimal

theme_linedraw

Practice